package org.jing.core.logger.logback;

import ch.qos.logback.classic.LoggerContext;
import org.jing.core.logger.itf.JingLoggerItf;
import org.jing.core.logger.itf.JingLoggerLevelItf;
import org.jing.core.util.ClassUtil;

/**
 * Description: <br>
 *
 * @author bks <br>
 * @since 2021-08-16 <br>
 */
public class Logger implements JingLoggerItf {
    private final ch.qos.logback.classic.Logger logger;

    Logger(String name) {
        logger = ((LoggerContext) org.slf4j.LoggerFactory.getILoggerFactory()).getLogger(name);
    }

    Logger(Class clazz) {
        logger = ((LoggerContext) org.slf4j.LoggerFactory.getILoggerFactory()).getLogger(clazz);
    }

    ch.qos.logback.classic.Logger getLogback() {
        return logger;
    }

    @Override public void all(Object object) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.ALL.getLevel(), ClassUtil.cast2String(object), null, null);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void all(Throwable throwable) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.ALL.getLevel(), "", null, throwable);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void all(String msg, Object... parameters) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.ALL.getLevel(), msg, parameters, null);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void all(Throwable throwable, String msg) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.ALL.getLevel(), msg, null, throwable);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void all(Throwable throwable, String msg, Object... parameters) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.ALL.getLevel(), msg, parameters, throwable);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void trace(Object object) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.TRACE.getLevel(), ClassUtil.cast2String(object), null, null);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void trace(Throwable throwable) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.TRACE.getLevel(), "", null, throwable);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void trace(String msg, Object... parameters) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.TRACE.getLevel(), msg, parameters, null);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void trace(Throwable throwable, String msg) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.TRACE.getLevel(), msg, null, throwable);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void trace(Throwable throwable, String msg, Object... parameters) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.TRACE.getLevel(), msg, parameters, throwable);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void debug(Object object) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.DEBUG.getLevel(), ClassUtil.cast2String(object), null, null);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void debug(Throwable throwable) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.DEBUG.getLevel(), "", null, throwable);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void debug(String msg, Object... parameters) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.DEBUG.getLevel(), msg, parameters, null);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void debug(Throwable throwable, String msg) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.DEBUG.getLevel(), msg, null, throwable);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void debug(Throwable throwable, String msg, Object... parameters) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.DEBUG.getLevel(), msg, parameters, throwable);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void info(Object object) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.INFO.getLevel(), ClassUtil.cast2String(object), null, null);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void info(Throwable throwable) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.INFO.getLevel(), "", null, throwable);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void info(String msg, Object... parameters) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.INFO.getLevel(), msg, parameters, null);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void info(Throwable throwable, String msg) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.INFO.getLevel(), msg, null, throwable);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void info(Throwable throwable, String msg, Object... parameters) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.INFO.getLevel(), msg, parameters, throwable);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void warn(Object object) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.WARN.getLevel(), ClassUtil.cast2String(object), null, null);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void warn(Throwable throwable) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.WARN.getLevel(), "", null, throwable);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void warn(String msg, Object... parameters) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.WARN.getLevel(), msg, parameters, null);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void warn(Throwable throwable, String msg) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.WARN.getLevel(), msg, null, throwable);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void warn(Throwable throwable, String msg, Object... parameters) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.WARN.getLevel(), msg, parameters, throwable);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void imp(Object object) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.IMP.getLevel(), ClassUtil.cast2String(object), null, null);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void imp(Throwable throwable) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.IMP.getLevel(), "", null, throwable);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void imp(String msg, Object... parameters) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.IMP.getLevel(), msg, parameters, null);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void imp(Throwable throwable, String msg) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.IMP.getLevel(), msg, null, throwable);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void imp(Throwable throwable, String msg, Object... parameters) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.IMP.getLevel(), msg, parameters, throwable);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void sql(Object object) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.SQL.getLevel(), ClassUtil.cast2String(object), null, null);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void sql(Throwable throwable) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.SQL.getLevel(), "", null, throwable);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void sql(String msg, Object... parameters) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.SQL.getLevel(), msg, parameters, null);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void sql(Throwable throwable, String msg) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.SQL.getLevel(), msg, null, throwable);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void sql(Throwable throwable, String msg, Object... parameters) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.SQL.getLevel(), msg, parameters, throwable);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void error(Object object) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.ERROR.getLevel(), ClassUtil.cast2String(object), null, null);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void error(Throwable throwable) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.ERROR.getLevel(), "", null, throwable);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void error(String msg, Object... parameters) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.ERROR.getLevel(), msg, parameters, null);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void error(Throwable throwable, String msg) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.ERROR.getLevel(), msg, null, throwable);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void error(Throwable throwable, String msg, Object... parameters) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.ERROR.getLevel(), msg, parameters, throwable);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void fatal(Object object) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.FATAL.getLevel(), ClassUtil.cast2String(object), null, null);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void fatal(Throwable throwable) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.FATAL.getLevel(), "", null, throwable);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void fatal(String msg, Object... parameters) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.FATAL.getLevel(), msg, parameters, null);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void fatal(Throwable throwable, String msg) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.FATAL.getLevel(), msg, null, throwable);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void fatal(Throwable throwable, String msg, Object... parameters) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.FATAL.getLevel(), msg, parameters, throwable);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void off(Object object) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.OFF.getLevel(), ClassUtil.cast2String(object), null, null);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void off(Throwable throwable) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.OFF.getLevel(), "", null, throwable);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void off(String msg, Object... parameters) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.OFF.getLevel(), msg, parameters, null);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void off(Throwable throwable, String msg) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.OFF.getLevel(), msg, null, throwable);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void off(Throwable throwable, String msg, Object... parameters) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, LoggerLevel.OFF.getLevel(), msg, parameters, throwable);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void log(JingLoggerLevelItf level, Object object) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, ((LoggerLevel) level).getLevel(), ClassUtil.cast2String(object), null, null);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void log(JingLoggerLevelItf level, Throwable throwable) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, ((LoggerLevel) level).getLevel(), "", null, throwable);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void log(JingLoggerLevelItf level, String msg, Object... parameters) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, ((LoggerLevel) level).getLevel(), msg, parameters, null);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void log(JingLoggerLevelItf level, Throwable throwable, String msg) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, ((LoggerLevel) level).getLevel(), msg, null, throwable);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }

    @Override public void log(JingLoggerLevelItf level, Throwable throwable, String msg, Object... parameters) {
        try {
            LoggerFactory.logMethod.invoke(logger, logger.getName(), null, ((LoggerLevel) level).getLevel(), msg, parameters, throwable);
        }
        catch (Throwable t) {
            logger.error("logger frame doesn't work", t);
        }
    }
}
